package com.iyimo.control{
	import com.iyimo.manager.PopUpManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class Alert extends BasePanel{
		private static var _ins:Alert = new Alert();
		
		private var _titleTxt:TextField;
		private var _contentTxt:TextField;
		
		private var _confirmFun:Function;
		private var _cancelFun:Function;
		
		public function Alert(){
			var s:MovieClip = new MovieClip();
			s.graphics.beginFill(0x000000, .8);
			s.graphics.drawRoundRect(0, 0, 300, 150, 10, 10);
			s.graphics.endFill();
			s.filters = [new GlowFilter(0xFFFFFF)];
			addChild(s);
			super(s);
			
			_titleTxt = new TextField();
			_titleTxt.defaultTextFormat = new TextFormat(null, 20);
			_titleTxt.selectable = false;
			_titleTxt.autoSize = TextFieldAutoSize.CENTER;
			_titleTxt.y = 10;
			addChild(_titleTxt);
			_contentTxt = new TextField();
			_contentTxt.multiline = _contentTxt.wordWrap = true;
			_contentTxt.selectable = false;
			_contentTxt.y = 40;
			addChild(_contentTxt);
			
			addEventListener(MouseEvent.CLICK, onClickPanel);
		}
		
		public static function show(title:String, content:String, modal:Boolean = false, confirmFun:Function = null, cancelFun:Function = null):void{
			PopUpManager.addPopUp(_ins, modal);
			_ins.title = title;
			_ins.content = content;
			_ins._confirmFun = confirmFun;
			_ins._cancelFun = cancelFun;
		}
		
		public static function close():void{
			PopUpManager.removePopUp(_ins);
		}
		
		public function get title():String{
			return _titleTxt.text;
		}
		
		public function set title(value:String):void{
			_titleTxt.text = value;
		}
		
		public function get content():String{
			return _contentTxt.text;
		}
		
		public function set content(value:String):void{
			_contentTxt.text= value;
		}
		
		override protected function onClose(e:Event = null):void{
			super.onClose(e);
		}
		
		override protected function onConfirm(e:Event = null):void{
			super.onConfirm(e);
			close();
			if (_confirmFun != null)
				_confirmFun.call();
		}
		
		override protected function onCancle(e:Event = null):void{
			super.onCancle(e);
			close();
			if (_cancelFun != null)
				_cancelFun.call();
		}
		
		/**
		 * 点击整个面板
		 * 如果只有确定,就不显示确认.并且点击面板即触发确认功能
		 */		
		private function onClickPanel(e:MouseEvent):void{
			if (_cancelFun != null)return;
			onConfirm();
		}
	}
}